<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Objekte &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Inhalt</a></h3>
  <ul>
<li><a class="reference internal" href="#">Objekte</a><ul>
<li><a class="reference internal" href="#aufgabe-1">Aufgabe 1</a></li>
<li><a class="reference internal" href="#aufgabe-2">Aufgabe 2</a></li>
<li><a class="reference internal" href="#aufgabe-3">Aufgabe 3</a></li>
</ul>
</li>
</ul>

  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/aufgabe_rst/Aufgaben_PowerShell_Objekte.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="objekte">
<h1>Objekte<a class="headerlink" href="#objekte" title="Permalink zu dieser Überschrift">¶</a></h1>
<div class="section" id="aufgabe-1">
<h2>Aufgabe 1<a class="headerlink" href="#aufgabe-1" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Passen Sie das folgende Skriot so an, dass die Informationen nicht lediglich per Write-Host ausgegeben werden, sondern in Form eines Objektes gespeichert werden.</p>
<div class="highlight-sh"><div class="highlight"><pre>        Function SysInfo
<span class="o">{</span>
        <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem <span class="sb">`</span>
        -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> -ComputerName <span class="nv">$strComputer</span>

        foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
          Write-Host <span class="s2">&quot;Computer Manufacturer: &quot;</span> <span class="nv">$objItem</span>.Manufacturer
          Write-Host <span class="s2">&quot;Computer Model: &quot;</span> <span class="nv">$objItem</span>.Model
          Write-Host <span class="s2">&quot;Total Memory: &quot;</span> <span class="nv">$objItem</span>.TotalPhysicalMemory <span class="s2">&quot;bytes&quot;</span>
        <span class="o">}</span>
<span class="o">}</span>

Function BIOSInfo <span class="o">{</span>

        <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_BIOS -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> <span class="sb">`</span>
        -computername <span class="nv">$strComputer</span>
        foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
                Write-Host <span class="s2">&quot;BIOS:&quot;</span><span class="nv">$objItem</span>.Description
                Write-Host <span class="s2">&quot;Version:&quot;</span><span class="nv">$objItem</span>.SMBIOSBIOSVersion<span class="s2">&quot;.&quot;</span>
                <span class="nv">$objItem</span>.SMBIOSMajorVersion<span class="s2">&quot;.&quot;</span><span class="nv">$objItem</span>.SMBIOSMinorVersion
                Write-Host <span class="s2">&quot;Serial Number:&quot;</span> <span class="nv">$objItem</span>.SerialNumber
        <span class="o">}</span>
<span class="o">}</span>


Function OSInfo
<span class="o">{</span>
        <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_OperatingSystem -Namespace <span class="sb">`</span> <span class="s2">&quot;root\CIMV2&quot;</span> -Computername <span class="nv">$strComputer</span>

        foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
                Write-Host <span class="s2">&quot;Operating System:&quot;</span> <span class="nv">$objItem</span>.Name
        <span class="o">}</span>
<span class="o">}</span>


Function CPUInfo
<span class="o">{</span>
        <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_Processor -Namespace <span class="sb">`</span>
        <span class="s2">&quot;root\CIMV2&quot;</span> -Computername <span class="nv">$strComputer</span>

        foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
                Write-Host <span class="s2">&quot;Processor:&quot;</span> <span class="nv">$objItem</span>.DeviceID <span class="nv">$objItem</span>.Name
        <span class="o">}</span>
<span class="o">}</span>


Function DiskInfo
<span class="o">{</span>
        <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_DiskDrive -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> <span class="sb">`</span> -ComputerName <span class="nv">$strComputer</span>

        foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
                Write-Host <span class="s2">&quot;Disk:&quot;</span> <span class="nv">$objItem</span>.DeviceID
                Write-Host <span class="s2">&quot;Size:&quot;</span> <span class="nv">$objItem</span>.Size <span class="s2">&quot;bytes&quot;</span>
                Write-Host <span class="s2">&quot;Drive Type:&quot;</span> <span class="nv">$objItem</span>.InterfaceType
                Write-Host <span class="s2">&quot;Media Type: &quot;</span> <span class="nv">$objItem</span>.MediaType
        <span class="o">}</span>
<span class="o">}</span>


Function NetworkInfo
<span class="o">{</span>
        <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_NetworkAdapterConfiguration <span class="sb">`</span> -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> -ComputerName <span class="nv">$strComputer</span> | where<span class="o">{</span><span class="nv">$_</span>.IPEnabled -eq <span class="s2">&quot;True&quot;</span><span class="o">}</span>

        foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
                Write-Host <span class="s2">&quot;DHCP Enabled:&quot;</span> <span class="nv">$objItem</span>.DHCPEnabled
                Write-Host <span class="s2">&quot;IP Address:&quot;</span> <span class="nv">$objItem</span>.IPAddress
                Write-Host <span class="s2">&quot;Subnet Mask:&quot;</span> <span class="nv">$objItem</span>.IPSubnet
                Write-Host <span class="s2">&quot;Gateway:&quot;</span> <span class="nv">$objItem</span>.DefaultIPGateway
                Write-Host <span class="s2">&quot;MAC Address:&quot;</span> <span class="nv">$ojbItem</span>.MACAddress
        <span class="o">}</span>
<span class="o">}</span>


<span class="c">#*=============================================================</span>
<span class="c">#* SCRIPT BODY</span>
<span class="c">#*=============================================================</span>
<span class="c">#* Connect to computer</span>
<span class="nv">$strComputer</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span>

<span class="c">#* Call SysInfo Function</span>
Write-Host <span class="s2">&quot;Sytem Information&quot;</span>
SysInfo
Write-Host

<span class="c">#* Call BIOSinfo Function</span>
Write-Host <span class="s2">&quot;System BIOS Information&quot;</span>
BIOSInfo
Write-Host

<span class="c">#* Call OSInfo Function</span>
Write-Host <span class="s2">&quot;Operating System Information&quot;</span>
OSInfo
Write-Host

<span class="c">#* Call CPUInfo Function</span>
Write-Host <span class="s2">&quot;Processor Information&quot;</span>
CPUInfo
Write-Host

<span class="c">#* Call DiskInfo Function</span>
Write-Host <span class="s2">&quot;Disk Information&quot;</span>
DiskInfo
Write-Host

<span class="c">#* Call NetworkInfo Function</span>
Write-Host <span class="s2">&quot;Network Information&quot;</span>
NetworkInfo
Write-Host
</pre></div>
</div>
</div>
<div class="section" id="aufgabe-2">
<h2>Aufgabe 2<a class="headerlink" href="#aufgabe-2" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Ihn ihrer Firma wird ein Skript genutzt, welches die Aufrufe der einzelnen Webseiten pro Wochentag aus den Log-Files des Webservers erstellt und in einer Datei &#8220;weekly_Stats.csv&#8221; speichert.</p>
<p>Die Struktur der Datei sieht wie folgt aus:</p>
<p>site_url, monday, tuesday, wednesday, thursday, friday, saturday, sunday
index.php,20000,14324,23453,43213,56432,23451,12343
login/login.php, 23432,34321,34567,43567,5432,43256,0
login/logout.php,12345,22345,32456,4232,3453,23456,1234
....</p>
<p>Insgesamt ist von ca 700 Zeilen in der Datei auszugehen.</p>
<p>Ihr Chef möchte nun als weitere Information für die jeweiligen urls den wöchentlichen Durchschnitt pro url berechnet bekommen und eine nach dem Durchschnitt sortierte Ausgabe in einer Textdatei speichern.</p>
<p>Erstellen Sie ein entsprechendes Skript. Benutzen Sie wenn möglich objektorientierte Ansätze sowie die Pipeline.</p>
</div>
<div class="section" id="aufgabe-3">
<h2>Aufgabe 3<a class="headerlink" href="#aufgabe-3" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Gegeben ist folgendes PS-Skript.</p>
<div class="highlight-sh"><div class="highlight"><pre>Function Get-NetworkConfiguration
<span class="o">{</span>
    param <span class="o">(</span>
        <span class="o">[</span>parameter<span class="o">(</span>
            <span class="nv">ValueFromPipeline</span><span class="o">=</span><span class="nv">$true</span>,
            <span class="nv">ValueFromPipelineByPropertyName</span><span class="o">=</span><span class="nv">$true</span>,
            <span class="nv">Position</span><span class="o">=</span>0<span class="o">)]</span>
        <span class="o">[</span>Alias<span class="o">(</span><span class="s1">&#39;__ServerName&#39;</span>, <span class="s1">&#39;Server&#39;</span>, <span class="s1">&#39;Computer&#39;</span>, <span class="s1">&#39;Name&#39;</span><span class="o">)]</span>
        <span class="o">[</span>string<span class="o">[]]</span>
        <span class="nv">$ComputerName</span> <span class="o">=</span> <span class="nv">$env</span>:COMPUTERNAME,
        <span class="o">[</span>parameter<span class="o">(</span><span class="nv">Position</span><span class="o">=</span>1<span class="o">)]</span>
        <span class="o">[</span>System.Management.Automation.PSCredential<span class="o">]</span>
        <span class="nv">$Credential</span>
    <span class="o">)</span>
    process
    <span class="o">{</span>
        <span class="nv">$WMIParameters</span> <span class="o">=</span> @<span class="o">{</span>
            <span class="nv">Class</span> <span class="o">=</span> <span class="s1">&#39;Win32_NetworkAdapterConfiguration&#39;</span>
            <span class="nv">Filter</span> <span class="o">=</span> <span class="s2">&quot;IPEnabled = &#39;true&#39;&quot;</span>
            <span class="nv">ComputerName</span> <span class="o">=</span> <span class="nv">$ComputerName</span>
        <span class="o">}</span>

        <span class="k">if</span> <span class="o">(</span><span class="nv">$Credential</span> -ne <span class="nv">$Null</span><span class="o">)</span>
        <span class="o">{</span>
            <span class="nv">$WmiParameters</span>.Credential <span class="o">=</span> <span class="nv">$Credential</span>
        <span class="o">}</span>
        foreach <span class="o">(</span><span class="nv">$adapter</span> in <span class="o">(</span>Get-WmiObject @WMIParameters<span class="o">))</span>
        <span class="o">{</span>
            <span class="nv">$OFS</span> <span class="o">=</span> <span class="s1">&#39;, &#39;</span>
            Write-Host <span class="s2">&quot;Server: $($adapter.DNSHostName)&quot;</span>
            Write-Host <span class="s2">&quot;Adapter: $($adapter.Description)&quot;</span>
            Write-Host <span class="s2">&quot;IP Address: $($adapter.IpAddress)&quot;</span>
            Write-Host <span class="s2">&quot;Subnet Mask: $($adapter.IPSubnet)&quot;</span>
            Write-Host <span class="s2">&quot;Default Gateway: $($adapter.DefaultIPGateway)&quot;</span>
            Write-Host <span class="s2">&quot;DNS Servers: $($adapter.DNSServerSearchOrder)&quot;</span>
            Write-Host <span class="s2">&quot;DNS Domain: $($adapter.DNSDomain)&quot;</span>
            Write-Host
        <span class="o">}</span>

    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<ul>
<li><p class="first">Wählen Sie statt der Write-Host-Ausgabe einen objektorientierten Ansatz</p>
</li>
<li><p class="first">Sie wollen nicht alle Informationen ausgeben, sondern beispielsweise nur die IP-Adresse und die Subnet-Maske</p>
</li>
<li><p class="first">Können Sie mit diesem Skript mehrere Rechner abfragen ?</p>
</li>
<li><p class="first">Welche Rechner ihres Netzes benutzen den gleichen DNS-Server mit der
IP-Adresse <strong>*.*</strong>.***.***. Eine Liste der Computer erhalten Sie mit Hilfe des CommandLets get-adcomputer</p>
</li>
<li><p class="first">Wie viele Rechner benutzen den gleichen Default-Gateway</p>
</li>
<li><p class="first">Übersetzen Sie den folgenden Text:</p>
<blockquote>
<div><p>When you begin to output options from your functions, the reusability and extensibility options soar. Objects allow you to take advantage of the pipeline (which allows your objects to be the input to other commands), allow your commands to use incoming objects (via the Parameter attribute and ValueFromPipelineByPropertyName switch), and most importantly, allow you to leverage the formatting and output system that Windows PowerShell provides, which keeps you from having to design your own output options and saves your effort for the real work—getting your job done!</p>
</div></blockquote>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>